package com.byb.server.web.rest;

import com.byb.server.service.dto.Wc.WcITDTO;
import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.WcInsuranceCategoryService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.WcInsuranceCategoryDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing WcInsuranceCategory.
 */
@RestController
@RequestMapping("/api")
public class WcInsuranceCategoryResource {

    private final Logger log = LoggerFactory.getLogger(WcInsuranceCategoryResource.class);

    private static final String ENTITY_NAME = "wcInsuranceCategory";

    private final WcInsuranceCategoryService wcInsuranceCategoryService;

    public WcInsuranceCategoryResource(WcInsuranceCategoryService wcInsuranceCategoryService) {
        this.wcInsuranceCategoryService = wcInsuranceCategoryService;
    }

    /**
     * POST  /wc-insurance-categories : Create a new wcInsuranceCategory.
     *
     * @param wcInsuranceCategoryDTO the wcInsuranceCategoryDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new wcInsuranceCategoryDTO, or with status 400 (Bad Request) if the wcInsuranceCategory has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/wc-insurance-categories")
    @Timed
    public ResponseEntity<WcInsuranceCategoryDTO> createWcInsuranceCategory(@RequestBody WcInsuranceCategoryDTO wcInsuranceCategoryDTO) throws URISyntaxException {
        log.debug("REST request to save WcInsuranceCategory : {}", wcInsuranceCategoryDTO);
        if (wcInsuranceCategoryDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new wcInsuranceCategory cannot already have an ID")).body(null);
        }
        WcInsuranceCategoryDTO result = wcInsuranceCategoryService.save(wcInsuranceCategoryDTO);
        return ResponseEntity.created(new URI("/api/wc-insurance-categories/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /wc-insurance-categories : Updates an existing wcInsuranceCategory.
     *
     * @param wcInsuranceCategoryDTO the wcInsuranceCategoryDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated wcInsuranceCategoryDTO,
     * or with status 400 (Bad Request) if the wcInsuranceCategoryDTO is not valid,
     * or with status 500 (Internal Server Error) if the wcInsuranceCategoryDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/wc-insurance-categories")
    @Timed
    public ResponseEntity<WcInsuranceCategoryDTO> updateWcInsuranceCategory(@RequestBody WcInsuranceCategoryDTO wcInsuranceCategoryDTO) throws URISyntaxException {
        log.debug("REST request to update WcInsuranceCategory : {}", wcInsuranceCategoryDTO);
        if (wcInsuranceCategoryDTO.getId() == null) {
            return createWcInsuranceCategory(wcInsuranceCategoryDTO);
        }
        WcInsuranceCategoryDTO result = wcInsuranceCategoryService.save(wcInsuranceCategoryDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, wcInsuranceCategoryDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /wc-insurance-categories : get all the wcInsuranceCategories.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of wcInsuranceCategories in body
     */
    @GetMapping("/wc-insurance-categories")
    @Timed
    public ResponseEntity<List<WcInsuranceCategoryDTO>> getAllWcInsuranceCategories(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of WcInsuranceCategories");
        Page<WcInsuranceCategoryDTO> page = wcInsuranceCategoryService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/wc-insurance-categories");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-insurance-categories/:id : get the "id" wcInsuranceCategory.
     *
     * @param id the id of the wcInsuranceCategoryDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the wcInsuranceCategoryDTO, or with status 404 (Not Found)
     */
    @GetMapping("/wc-insurance-categories/{id}")
    @Timed
    public ResponseEntity<WcInsuranceCategoryDTO> getWcInsuranceCategory(@PathVariable Long id) {
        log.debug("REST request to get WcInsuranceCategory : {}", id);
        WcInsuranceCategoryDTO wcInsuranceCategoryDTO = wcInsuranceCategoryService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(wcInsuranceCategoryDTO));
    }

    /**
     * DELETE  /wc-insurance-categories/:id : delete the "id" wcInsuranceCategory.
     *
     * @param id the id of the wcInsuranceCategoryDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/wc-insurance-categories/{id}")
    @Timed
    public ResponseEntity<Void> deleteWcInsuranceCategory(@PathVariable Long id) {
        log.debug("REST request to delete WcInsuranceCategory : {}", id);
        wcInsuranceCategoryService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/wc-insurance-categories?query=:query : search for the wcInsuranceCategory corresponding
     * to the query.
     *
     * @param query the query of the wcInsuranceCategory search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/wc-insurance-categories")
    @Timed
    public ResponseEntity<List<WcInsuranceCategoryDTO>> searchWcInsuranceCategories(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of WcInsuranceCategories for query {}", query);
        Page<WcInsuranceCategoryDTO> page = wcInsuranceCategoryService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/wc-insurance-categories");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-insurance-categories : get all the wcInsuranceCategories for Wc.
     * @return the ResponseEntity with status 200 (OK) and the list of wcInsuranceCategories in body
     */
    @GetMapping("/wc-insurance-category-group")
    @Timed
    public ResponseEntity<List<WcITDTO>> getAllWcInsuranceCategories() {
        log.debug("REST request to get a page of WcInsuranceCategories");
        List<WcITDTO> list = wcInsuranceCategoryService.findAllForWc();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

}
